package top.serms.leetcode;

public class LeetCode209_MinSubArrayLen {


    // 滑动窗口01
    public static int minSubArrayLen01(int target, int[] nums) {
        int left = 0;
        int right = 0;
        int sum = 0;
        int min = Integer.MAX_VALUE;
        while (right < nums.length) {
            sum += nums[right];     // 和
            while (sum >= target) {
                min = Math.min(min, right - left + 1);
                sum -= nums[left];
                left++;
            }
            right++;
        }
        return min == Integer.MAX_VALUE ? 0 : min;
    }

    // 滑动窗口02
    public static int minSubArrayLen02(int target, int[] nums) {
        int left = 0;
        int right = 0;
        int sum = 0;
        int minlength = 0;
        while (right < nums.length) {
            sum += nums[right];     // 和
            while (sum >= target) {
                if (right - left + 1 < minlength || minlength == 0) {
                    minlength = right - left + 1;
                }
                sum -= nums[left];
                left++;
            }
            right++;
        }
        return minlength;
    }

    public static void main(String[] args) {
        int[] arr = {2, 3, 1, 2, 4, 3};
        int i = minSubArrayLen02(7, arr);
        System.out.println(i);
    }
}
